package com.autoagent.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 数据库配置类
 */
@Configuration
@EnableTransactionManagement
public class DatabaseConfig implements MyBatisFlexCustomizer {

    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        // 开启审计功能
        AuditManager.setAuditEnable(true);
        
        // 设置 SQL 审计收集器
        MessageCollector collector = new ConsoleMessageCollector();
        AuditManager.setMessageCollector(collector);
        
        // 设置逻辑删除配置
        globalConfig.setLogicDeleteColumn("deleted");
        globalConfig.setLogicDeleteValue("1");
        globalConfig.setLogicNormalValue("0");
        
        // 设置版本控制字段
        globalConfig.setVersionColumn("version");
    }
    
    /**
     * 自定义消息收集器Bean
     */
    @Bean
    public MessageCollector messageCollector() {
        return new ConsoleMessageCollector();
    }
}